package com.apecircle.easytranslib;

import com.apecircle.easytranslib.bean.XlsWriteBean;
import com.apecircle.easytranslib.method.ReadXlsManager;
import com.apecircle.easytranslib.method.WriteXlsManager;

import java.io.File;
import java.util.*;

/**
 * 将谷歌反馈中的csv使用wps转换成xlsx,然后重命名为transfer.xlsx
 * 运行程序就会得到 transfer_finish.xlsx 文件 然后处理transfer_finish.xlsx使用 TranslateXls去翻译
 */
public class TransferXls {

    private static final String VALUE_PATH = "Test";  //value文件夹存放的名字
    private static final String XLS_NAME = "transfer.xlsx"; //要生成xls的名字
    private static final String XLS_NAME_FINISH = "transfer_finish.xlsx"; //要生成xls的名字

    public static void main(String[] args) {

        File file = new File("");
        // 当前路径
        String ROOT_PATH = file.getAbsolutePath();

        XlsWriteBean bean = new XlsWriteBean.Builder()
                .setRootPath(ROOT_PATH)
                .setFileFloderName(VALUE_PATH)
                .setXlsName(XLS_NAME)
                .setFinishFileName(XLS_NAME_FINISH)
                .builder();
        /**
         * 读取一个xls将其放在list中
         */
        LinkedHashMap<Integer, List<String>> stringList = ReadXlsManager.getInstance().readXlsToList(bean.getBuilder());
        HashMap<Integer,List<String>> map = new HashMap<>();
        String[] titles = ReadXlsManager.getInstance().getTitle();
        if(titles.length == 0){
            System.out.println("标题为空");
            return;
        }
        String[] title = titles[0].split(",");
        if(stringList.size() == 1){
            List<String> stringList1 = stringList.get(0);
            for (int i = 0 ; i < stringList1.size();i++){
                String[] split = stringList1.get(i).split(",");
                if(split.length != title.length){
                    continue;
                }
                //标题
                for (int j = 0; j < split.length; j++) {
                    List<String> strings = map.get(j);
                    if (strings == null) {
                        strings = new ArrayList<>();
                    }
                    strings.add(split[j]);
                    map.put(j, strings);
                }
            }

            List<Translate> lists = new ArrayList<>();
            for (Map.Entry<Integer,List<String>> entry: map.entrySet()){
                int key = entry.getKey();
                List<String> value = entry.getValue();
                lists.add(new Translate(title[key],value));
            }

            /**
             * 将新的list写入xls中
             */
            WriteXlsManager.getInstance().writeTranslateXls(bean.getBuilder(),lists);

        }



    }
}
